package org.classdef.analyzer.info;

import java.util.ArrayList;
import java.util.List;

import org.classdef.analyzer.info.accesstype.ClassAccessType;
import org.classdef.analyzer.info.attribute.AttributePool;
import org.classdef.analyzer.info.constant.ConstantPool;
import org.classdef.analyzer.info.field.FieldPool;
import org.classdef.analyzer.info.method.MethodPool;


/**
 * 
 * class格式解析后对应的类
 *
 */
public class ClassDef {

	private int magic;
	private short minor;
	private short major;
	private short constantPoolCount;
	private ConstantPool constantPool;
	private short accessFlags;
	private short thisClass;
	private short superClass;
	private short interfacesCount;
	//index of constant pool
	private List<Short> interfacesIndeies = new ArrayList<Short>();
	
	private short fieldCount;
	private FieldPool fieldPool;
	
	private short methodCount;
	private MethodPool methodPool;
	
	private short attributeCount;
	private AttributePool attributePool;
	
	/**
	 * @return magic
	 */
	public int getMagic() {
		return magic;
	}
	/**
	 * @param magic 要设置的 magic
	 */
	public void setMagic(int magic) {
		this.magic = magic;
	}
	/**
	 * @return minor
	 */
	public short getMinor() {
		return minor;
	}
	/**
	 * @param minor 要设置的 minor
	 */
	public void setMinor(short minor) {
		this.minor = minor;
	}
	/**
	 * @return major
	 */
	public short getMajor() {
		return major;
	}
	/**
	 * @param major 要设置的 major
	 */
	public void setMajor(short major) {
		this.major = major;
	}
	/**
	 * @return constantPoolCount
	 */
	public short getConstantPoolCount() {
		return constantPoolCount;
	}
	
	/**
	 * @param constantPoolCount 要设置的 constantPoolCount
	 */
	public void setConstantPoolCount(short constantPoolCount) {
		this.constantPoolCount = constantPoolCount;
	}
	/**
	 * @return constantPool
	 */
	public ConstantPool getConstantPool() {
		return constantPool;
	}
	/**
	 * @param constantPool 要设置的 constantPool
	 */
	public void setConstantPool(ConstantPool constantPool) {
		this.constantPool = constantPool;
	}
	/**
	 * @return accessFlags
	 */
	public short getAccessFlags() {
		return accessFlags;
	}
	/**
	 * @param accessFlags 要设置的 accessFlags
	 */
	public void setAccessFlags(short accessFlags) {
		this.accessFlags = accessFlags;
	}
	/**
	 * @return thisClass
	 */
	public short getThisClass() {
		return thisClass;
	}
	/**
	 * @param thisClass 要设置的 thisClass
	 */
	public void setThisClass(short thisClass) {
		this.thisClass = thisClass;
	}
	/**
	 * @return superClass
	 */
	public short getSuperClass() {
		return superClass;
	}
	/**
	 * @param superClass 要设置的 superClass
	 */
	public void setSuperClass(short superClass) {
		this.superClass = superClass;
	}
	/**
	 * @return interfacesCount
	 */
	public short getInterfacesCount() {
		return interfacesCount;
	}
	/**
	 * @param interfacesCount 要设置的 interfacesCount
	 */
	public void setInterfacesCount(short interfacesCount) {
		this.interfacesCount = interfacesCount;
	}
	/**
	 * @return interfacesIndex
	 */
	public short[] getInterfacesIndeies() {
		short[] indeies = new short[interfacesIndeies.size()];
		for (int i = 0; i < interfacesIndeies.size(); i++) {
			indeies[i] = interfacesIndeies.get(i);
		}
		return indeies;
	}
	/**
	 * @return fieldCount
	 */
	public short getFieldCount() {
		return fieldCount;
	}
	/**
	 * @param fieldCount 要设置的 fieldCount
	 */
	public void setFieldCount(short fieldCount) {
		this.fieldCount = fieldCount;
	}
	/**
	 * @return fieldPool
	 */
	public FieldPool getFieldPool() {
		return fieldPool;
	}
	/**
	 * @param fieldPool 要设置的 fieldPool
	 */
	public void setFieldPool(FieldPool fieldPool) {
		this.fieldPool = fieldPool;
	}
	/**
	 * @return methodCount
	 */
	public short getMethodCount() {
		return methodCount;
	}
	/**
	 * @param methodCount 要设置的 methodCount
	 */
	public void setMethodCount(short methodCount) {
		this.methodCount = methodCount;
	}
	/**
	 * @return methodPool
	 */
	public MethodPool getMethodPool() {
		return methodPool;
	}
	/**
	 * @param methodPool 要设置的 methodPool
	 */
	public void setMethodPool(MethodPool methodPool) {
		this.methodPool = methodPool;
	}
	/**
	 * @return attributeCount
	 */
	public short getAttributeCount() {
		return attributeCount;
	}
	/**
	 * @param attributeCount 要设置的 attributeCount
	 */
	public void setAttributeCount(short attributeCount) {
		this.attributeCount = attributeCount;
	}
	/**
	 * @return attributePool
	 */
	public AttributePool getAttributePool() {
		return attributePool;
	}
	/**
	 * @param attributePool 要设置的 attributePool
	 */
	public void setAttributePool(AttributePool attributePool) {
		this.attributePool = attributePool;
	}
	public void addInterfaceIndex(short index) {
		interfacesIndeies.add(index);
	}
	
	/* （非 Javadoc）
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		sb.append("\"magic\":").append(magic);
		sb.append(",\"minor\":").append(minor);
		sb.append(",\"major\":").append(major);
		sb.append(",\"constantPoolCount\":").append(constantPoolCount);
		sb.append(",\"constantPool\":").append(constantPool);
		sb.append(",\"accessFlags\":").append(accessFlags);
		sb.append(",\"access\":\"").append(ClassAccessType.getType(accessFlags)).append("\"");
		sb.append(",\"thisClass\":").append(thisClass);
		sb.append(",\"superClass\":").append(superClass);
		sb.append(",\"interfacesCount\":").append(interfacesCount);
		
		sb.append(",\"interfacesIndeies\":[");
		if(interfacesIndeies.size() > 0){
			for (short index : interfacesIndeies) {
				sb.append(index).append(",");
			}
			sb.setLength(sb.length() - 1);
		}
		sb.append("]");
		
		sb.append(",\"fieldCount\":").append(fieldCount);
		sb.append(",\"fieldPool\":").append(fieldPool);
		sb.append(",\"methodCount\":").append(methodCount);
		sb.append(",\"methodPool\":").append(methodPool);
		sb.append(",\"attributeCount\":").append(attributeCount);
		sb.append(",\"attributePool\":").append(attributePool);
		sb.append("}");
		return sb.toString();
	}
	
}
